// pages/login/login.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone_number:'',
    password:'',
    captcha:'',
    sole:0,
    codectr:''
  },
  createCode() {
    var code = ''; //初始化验证码
    var codeLength = 6; //设置验证码长度
    //设置随机字符
    var txt = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
    //循环
    for(var i = 0; i < codeLength; i++) {
      //设置随机数范围
      var index = Math.floor(Math.random() * 36);
      code += txt[index];
    }
    //动态设置数据
    this.setData({
      codectr:code,
    })
  },
  phone_number:function(e){
    this.setData({
      phone_number: e.detail.value
    })
    // console.log(this.data.phone_number)
  },
  password:function(e){
    this.setData({
      password: e.detail.value
    })
    // console.log(this.data.phone_number)
  },
  captcha:function(e){

    this.setData({captcha:e.detail.value});
  },
  lonin:function(){
    let phone_number = this.data.phone_number;
    let password = this.data.password;
    let captcha = this.data.captcha;
    // console.log(phone_number)
    // console.log(password)
    let reg =/\S/;
    console.log(this.data.codectr)
    if(reg.test(phone_number) == false){
        wx.showToast({
          title: '手机号不能为空',
          icon: 'error',
          duration: 1000,
        })
    }
    else if(reg.test(password) == false){
      wx.showToast({
        title: '密码不能为空',
        icon: 'error',
        duration: 1000,
      })
    }else if(captcha !=this.data.codectr){
      wx.showToast({
        title: '验证码错误',
        icon: 'error',
        duration: 1000,
      })
      this.setData({codectr:''})
      this.createCode();
    }else if(reg.test(phone_number) == true && reg.test(password) == true){
      wx.request({
        url: 'http://39.106.9.207:8080/warehouse/Login',
        data: {
          phone:phone_number,
          pwd:password,
        },
        method:'GET',
        header: {
          'content-type': 'multipart/form-data;charset=utf-8' // 默认值
        },
        success: (res) => {
          console.log(res)
          this.setData({
            sole: res.data
          })
          console.log(this.data.sole)
          let sole = this.data.sole;
          if (sole != 11 && sole != 12) {
            wx.showToast({
              title: '登录成功',
              icon: 'success',
              duration: 1000
            })
            this.setData({
              warehouseId: res.data.warehouseId
            })
            let warehouseId =res.data.warehouseId;
            let company_name = res.data.name;
            let whName = res.data.whName
            wx.setStorage({
              key:"warehouseId",
              data:warehouseId,
            });
            wx.setStorage({
              key:"company_name",
              data:company_name,
            });
            wx.setStorage({
              key:"whName",
              data:whName,
            });
            this.jump();
          }
          if (sole == 12) {
            wx.showToast({
              title: '密码错误',
              icon: 'error',
              duration: 1000
            })
          }
          if (sole == 11) {
            wx.showToast({
              title: '手机号不存在',
              icon: 'error',
              duration: 1000
            })
          }
        },
      });
      // console.log(this.data.sole)
    }
  },
  jump(){
    wx.reLaunch({
      url: '/pages/mine/mine'
    })
    },
  // goto:function(){
  //   wx.switchTab({
  //     url: '/pages/index/index',
  //   })
  // },
  register:function(){
    wx.navigateTo({
      url: '/pages/register/register',
    })
  },
  forget_password:function(){
    wx.navigateTo({
      url: '/pages/forget_password/forget_password',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.createCode();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})